<template>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品列表</el-breadcrumb-item>
</el-breadcrumb> <br> 
<el-card>
    <el-input placeholder="输入搜索的内容" v-model="user.query" @input="search"></el-input> <el-button>搜索</el-button> <el-button type="primary" @click="$router.push('/add')">添加商品</el-button><br> <br>
      <el-table :data="tableData" border style="width: 100%">
    <el-table-column  type="index" label="#"> </el-table-column>
    <el-table-column prop="goods_name" label="商品名称" > </el-table-column>
    <el-table-column  prop="goods_price" label="商品价格"> </el-table-column>
    <el-table-column prop="goods_weight" label="商品重量" > </el-table-column>
    <el-table-column label="创建时间" > 
      <template slot-scope="scope">
          {{scope.row.add_time | dateTime}}
      </template>
      </el-table-column> 
    <el-table-column prop="address"  label="操作"> 
          <template slot-scope="scope"> 
               <el-button type="primary" size="mini" >编辑</el-button> 
               <el-button type="danger"  size="mini" @click="del(scope.row.goods_id)">删除</el-button>  
          </template> 
    </el-table-column> 
  </el-table>
</el-card>

  <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="user.pagenum"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="user.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
    </div>
</template> 
<script>
import { getgoodsApi , delgoodsApi} from "@/http/api";
export default {
  data() {
    return {
      tableData: [], //表格的数据
      user: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    //点击编辑按钮
    bianji(row){
        console.log(row) 
        this.$router.push({path:"/adds",query:{row} })  
    },
    async getlist() {
      let res = await getgoodsApi(this.user);
      console.log(res);
      this.tableData = res.goods;
      this.total = res.total;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.user.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.user.pagenum = val;
      this.getlist();
    },
    //搜索
    search() {
      this.getlist();
    },
    //删除
    del(id) {
      console.log(id)
         this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
            await delgoodsApi(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getlist()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });        
        });
    },
  }
};
</script>

<style scoped >
.el-input {
  width: 350px;
}
</style>
